﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _18miljard.Partners;

namespace _18miljard.UI.Controls
{
	public partial class PartnersControl : BaseUserControl
	{
		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.DisplayPartners();
		}

		private void DisplayPartners()
		{
			PartnerCollection partners = PartnerCache.Partners.Partners;

			int cellsPerRow = 2;
			int cells = 0;
			TableRow currentRow = null;

			foreach (Partner partner in partners)
			{
				if (cells == 0)
				{
					currentRow = new TableRow();
					this.Table1.Rows.Add(currentRow);
				}

				TableCell cell = new TableCell();

				HyperLink link = new HyperLink();
				link.Target = "_blank";
				link.NavigateUrl = partner.Href;

				if (partner.LinkType == LinkType.Banner)
				{
					link.ImageUrl = partner.Banner;
					link.Text = partner.Alt;

					cell.Controls.Add(link);
				}
				else
				{
					link.Text = partner.Text;
					link.CssClass = "partnerlink";

					if (string.IsNullOrEmpty(partner.Prefix) == false)
					{
						Label prefix = new Label();
						prefix.Text = partner.Prefix;
						prefix.CssClass = "partnerlink";

						cell.Controls.Add(prefix);
					}

					cell.Controls.Add(link);

					if (string.IsNullOrEmpty(partner.Suffix) == false)
					{
						Label suffix = new Label();
						suffix.Text = partner.Suffix;
						suffix.CssClass = "partnerlink";

						cell.Controls.Add(suffix);
					}
				}

				currentRow.Cells.Add(cell);

				cells++;
				if (cells == cellsPerRow)
					cells = 0;
			}
		}
	}
}